package flib;

import importExport.SVGLoader;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Vector;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import processing.core.PApplet;
import processing.core.PImage;

import simpleGeom.Bounds2D;
import simpleGeom.Bounds3D;
import simpleGeom.HENode;
import simpleGeom.HEdge;
import simpleGeom.HasPos2D;
import simpleGeom.Polygon2D;
import simpleGeom.Rect2D;
import simpleGeom.Vec2D;

public class FlibLevel extends FlibGraph{
	PApplet parent;
	PImage image;
	String filename;
	Vector<FlibLevel> children;
	float z;
	FlibLevel upGroup;
	FlibLevel downGroup;
	Vector<Stair> stairs=new Vector<Stair>();
	Vector<Polygon2D>outlines=new Vector<Polygon2D>();
	
	public FlibLevel(){
		
	}
	
	public FlibLevel(FlibGraph original) {
		super(original);
		// TODO Auto-generated constructor stub
	}

	public void createGraph() {
		intersect();
		removeDoubleWalls();
		for (int i = 0; i < walls.size(); i++) {
			FlibWall wall = walls.get(i);
			wall.hEdge.attach();
		}
	}
	
	public FlibFace findRooms(Vector<? extends HasPos2D> points, float tolerance) {
		Bounds2D b = new Bounds2D();
		b.addAll(points);
		float cArea = b.getArea();
		for (int i = 0; i < rooms.size(); i++) {
			FlibFace room = rooms.get(i);
			Bounds2D roomBounds = (Bounds2D) room.getBounds2D();
			float roomArea = roomBounds.getArea();
			if (Math.abs(roomArea - cArea) < tolerance) {
				if (Math.abs(roomBounds.getOverlapArea(b) - roomArea) < tolerance) {
					return room;
				}
			}
		}
		return null;
	}

	public Rect2D bound2d() {
		Bounds2D b2d = new Bounds2D();
		for (int i = 0; i < rooms.size(); i++) {
			FlibFace room = rooms.get(i);
			b2d.addRect2D(room.calculateBounds());
		}
		return b2d;
	}

	public void removeDoubleWalls() {
		for (int i = 0; i < walls.size(); i++) {
			FlibWall wall = walls.get(i);
			wall.checked = false;
		}
		Vector<FlibWall> newWalls = new Vector<FlibWall>();
		for (int i = 0; i < walls.size(); i++) {
			FlibWall w1 = walls.get(i);
			Vector<FlibWall> doubleWalls = new Vector<FlibWall>();
			if (!w1.checked) {
				doubleWalls.add(w1);
				w1.checked = true;
			}

			for (int ii = i + 1; ii < walls.size(); ii++) {
				FlibWall w2 = walls.get(ii);
				if (!w2.checked) {
					if ((w1.getNode1() == w2.getNode1() && w1.getNode2() == w2
							.getNode2())
							|| (w1.getNode2() == w2.getNode1() && w1.getNode1() == w2
									.getNode2())) {
						w2.checked = true;
						doubleWalls.add(w2);
					}
				}
			}
			if (doubleWalls.size() == 1) {
				newWalls.add(doubleWalls.get(0));
			} else {
				for (int j = 0; j < doubleWalls.size(); j++) {
					FlibWall wall = doubleWalls.get(j);
					if (wall.type != null) {
						if (wall.type.permeable || wall.type.transparent) {
							newWalls.add(wall);
							j = doubleWalls.size();
							break;
						}
						if (j == doubleWalls.size() - 1) {
							newWalls.add(wall);
						}
					}
				}
			}

		}
		System.out.println(walls.size());
		walls = newWalls;
		System.out.println(walls.size());
	}

	public PImage getImage() {
		return image;
	}

	public void setImage(PImage image) {
		this.image = image;
	}

	

	

	

	


}
